package com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.ResistEarthquakesCalculationParam;
import com.css.fxfzfxqh.modules.comprehensiveSearch.dto.CalculationResultListDto;
import com.css.fxfzfxqh.modules.comprehensiveSearch.service.CalculationResultService;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.dto.FxqhAssessReportBuildReportDto;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.param.AssessExportParam;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.service.FxqhAssessReportService;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("assessReport")
public class FxqhAssessReportController {

    @Autowired
    private FxqhAssessReportService fxqhAssessReportService;
    @Autowired
    private CalculationResultService calculationResultService;

    /**
     * 生成报告编号
     *
     * @return
     */
    @GetMapping("buildReportCode")
    public RestResponse<String> buildReportCode() {
        return RestResponse.succeed(fxqhAssessReportService.buildReportCode());
    }

    /**
     * 生成报告
     *
     * @return
     */
    @PostMapping("buildReport")
    public RestResponse<?> buildReport(@Valid @RequestBody FxqhAssessReportBuildReportDto dto) throws BizException {
        dto.setCreateUser(PlatformSessionContext.getUserID());
        dto.setCreateTime(new Date());
        fxqhAssessReportService.buildReport(dto);
        return RestResponse.succeed();
    }

    /**
     * 查询列表
     *
     * @param code
     * @param name
     * @return
     */
    @GetMapping("list")
    public RestResponse<?> list(String code, String name) {
        return RestResponse.succeed(fxqhAssessReportService.list(code, name));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("delete")
    public RestResponse<?> delete(@RequestParam String id) {
        fxqhAssessReportService.delete(id, PlatformSessionContext.getUserID());
        return RestResponse.succeed();
    }

    /**
     * 上传
     *
     * @param templateId
     * @param fileId
     * @return
     */
    @GetMapping("upload")
    public RestResponse<?> upload(@RequestParam String templateId, @RequestParam String fileId) {
        fxqhAssessReportService.upload(templateId, fileId, PlatformSessionContext.getUserID());
        return RestResponse.succeed();
    }

    /**
     * 查询评估记录列表
     *
     * @return
     */
    @GetMapping("records")
    public RestResponse<List<CalculationResultListDto>> records() {
        return RestResponse.succeed(calculationResultService.assessRecordsForBuildReport());
    }

    /**
     * 导出
     *
     * @param param 参数
     */
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, AssessExportParam param) {
        fxqhAssessReportService.exportData(response, param);
    }


}
